#!/usr/bin/php -q
<?php
    /*
    Description:
    -File save token: kurtulus_autobackup_config/tok.kurtulus
    -Update token(chưa refresh ổn): authorization.php
    -Tên thư mục được tạo: /backup/20xx-xx-xx/
    -File backup: /backup/20xx-xx-xx.tar
    Log:
    -Jan 30, 2013, Sonhb - Create 
    -Jan 31, 2013, sonhb - Add send mail function
    */
    error_reporting(0);
    ini_set("memory_limit","-1");
    function __realpath($path) {
        $path = str_replace('\\','/',realpath($path));
        if ($path{1} == ':') {
            // We can't just check for C:/, because windows users may have the IIS webroot on X: or F:, etc.
            $path = substr($path,2);
        }
        return $path;
    }
    define('EXPONENT', "ABSoft - Auto backup data");
    
    // define realpath
    $path = __realpath(dirname(__FILE__));
    $temp = explode("/", $path);
    unset($temp[count($temp)-1]);
    $path = implode("/", $temp);
    
    define('BASE',"{$path}/");
    
    $count_time=time();
    include(BASE."auto_backup/func.php");
    
    
    include(BASE."conf/config.php");
    include(BASE."auto_backup/kurtulus_autobackup_config/config.php");
    include(BASE.'subsystems/database.php');
    include(BASE."auto_backup/drive.php");

    database_connect(DB_HOST,DB_USER,DB_PASS,DB_NAME);
    
    print("Connecting to Database\r\n");
    //For exponent  
    $db=null;  
    $db=exponent_database_connect(DB_USER,DB_PASS,DB_HOST.':'.DB_PORT,DB_NAME,'',true);
    $time=date('Y-m-d');
    if ($db)
        print("Connected to database\r\n");
    
    print("Virus scanning.....\r\n");
    //Scan virus
    scan_virus(BASE.'files/');
    
    // check backup dir
    if (!is_dir(BASE."backup"))
    {
        mkdir(BASE."backup");
        chmod (BASE."backup",0775);
    }
    
    $backup_dir=BASE.'auto_backup/backup/'.$time;
    if(!is_dir($backup_dir))
    {
        print "Create directory {$backup_dir}\r\n";
        mkdir($backup_dir, true);
        chmod ($backup_dir,0775);
    }
    
    print("Backing up files folder.....\r\n");
    
    //Create compress files  
    tar_bz2_file(BASE.'files',$backup_dir.'/files');
    print("Create compress files complete\r\n");
    
    print("Database exporting.....\r\n");
    //Create compress database
    backup_tables(DB_HOST,DB_USER,DB_PASS,DB_NAME);
    
    print("Create compress database complete\r\n");
    
    print("Uploading to Google Drive.....\r\n");
    //Tar files  
    tar_file($backup_dir,BASE.'auto_backup/backup/'.$time);
    
    // Split file into multi parts
    print("Spliting file.....\r\n");
    split_file($backup_dir.'.tar',35);
    //Create folder
    print("Create new folder on Google Drive.....\r\n");
    $folder_id=create_folder($foldername);
    //Google drive
    print("Begin upload files\r\n");
    $files = glob($backup_dir.'.tar.part-*');
    $mail_content = "Hi<br> On ".date('l jS \of F Y h:i:s A').", system was backuped. Backup files had been uploaded to Google Drive. You can login to Google Drive of kurtulus.partners@gmail.com to manage all of them.<br>Here is the links to download :<br><br>";
    foreach ($files as $file)
    {
        print "Uploading {$file}... \r\n";
        $file_info=uploadto_ggdrive($file,'Backup_',$folder_id);
        $mail_content.="File {$file_info->name}<br>File size: {$file_info->size}<br>Download link: <a href='{$file_info->link}'>{$file_info->link}</a><br><br>";
    }
    print("Upload complete\r\n");
    print("Clear folder\r\n");
    sendmail('Kurtulus & Partners website backup completed on '.date('l jS \of F Y h:i:s A'),$mail_content);  
    echo "Total time:".(time()-$count_time)."s\r\n";
?>
